//只配置链接
const express = require('express');
const router = express.Router();

const multer = require("multer");
const fs = require("fs");

//引入控制器
const UserController = require("../controller/UserControllet");

//引入用户名
// http://localhost:3008/user/userlist
router.get('/userlist', UserController.Userlist);

// 注册
router.post("/register", UserController.UserRegister);

// 登录
router.post("/login", UserController.Login);

// 图片验证码
router.get("/captcha", UserController.svgCaptcha);

// 短信随机验证码
// http://localhost:3008/user/sendcode
router.post("/sendcode", UserController.MessageCode);

// 短信登陆接口
// http://localhost:3008/user/phonelogin
router.post("/phonelogin", UserController.PhoneLogin);

// 新用户注册
router.post("/adduser", UserController.Adduser);

// 修改用户接口
router.post("/edituser", UserController.edituser);

// 修改is_show接口
router.post("/editshow", UserController.editisShow);

// 删除用户接口
router.get("/deleteuser", UserController.deleUser);

// 查询所有数据  alluser
router.get("/allusers", UserController.alluser);

// 上传图片
router.post("/upload", multer({
    dest: "public/uploads" //设置文件存储路径
}).array("file", 1), function(req, res) {
    console.log(req.files);

    // {
    //     fieldname: 'file',
    //     originalname: 'psb.jpg',
    //     encoding: '7bit',
    //     mimetype: 'image/jpeg',
    //     destination: '../public/uploads',
    //     filename: '58c42e6c291238c33e6f87ebeb74704e',
    //     path: '..\\public\\uploads\\58c42e6c291238c33e6f87ebeb74704e',
    //     size: 120984
    //   }

    let file = req.files[0];
    let fileData = {};
    let imgPath = "public/uploads/" + file.originalname;
    fs.renameSync("public/uploads/" + file.filename, imgPath);
    fileData.type = file.mimentype;
    fileData.name = imgPath;
    fileData, size = file.size;
    fileData.path = file.path;
    res.json({
        status: 200,
        msg: "successed",
        data: fileData
    })
})

module.exports = router;